package com.hospital.service.sample.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hospital.service.project.model.param.ProGroupInfoQueryListParam;
import common.core.util.SystemLog;
import enhance.plus.logic.LogicBaseController;
import com.hospital.service.sample.entity.SamInfoBase;
import com.hospital.service.sample.entity.help.SamInfoBaseHelp;
import com.hospital.service.sample.model.param.SamInfoBaseParam;
import com.hospital.service.sample.service.SamInfoBaseService;
import com.hospital.service.sample.model.param.SamInfoBaseQueryForOneRowParam;
import com.hospital.service.sample.model.param.SamInfoBaseQueryPageParam;
import com.hospital.service.sample.model.vo.SamInfoBaseVo;
import common.core.util.CommonUtil;
import common.core.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import enhance.plus.logic.LogicBaseController;
import enhance.plus.util.Page;
import enhance.plus.util.PageHelper;


import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;


/**
 * 实验样本基本表 控制器
 *
 * @author lfb
 * @created 2020-10-10 14:26:39
 */
@Slf4j
@Validated
@RestController
@Api(tags = "实验样本基本表")
@RequestMapping("/sample/samInfoBase")
public class SamInfoBaseController extends LogicBaseController<SamInfoBaseService, SamInfoBase> {

    @Autowired
    private SamInfoBaseService samInfoBaseService;

    @ApiOperation(value = "样本检索编号生成")
    @RequestMapping(value = "/getSamplesNumber" , method = RequestMethod.GET)
    public Result getSamplesNumber(@Valid SamInfoBaseParam param){
        log.debug("开始生成样本检索编号-->{}",param);
        return Result.success(this.samInfoBaseService.getSamplesNumber());
    }

    @ApiOperation(value = "保存实验样本基本表的数据")
    @SystemLog(value = "保存实验样本", logo = "samplesNumber")
    @RequestMapping(value = "/save" , method = RequestMethod.PUT)
    public Result save(@Valid @RequestBody SamInfoBaseParam param){
        log.debug("开始保存实验样本基本表-->{}",param);
        this.samInfoBaseService.add(CommonUtil.copyBean(param,SamInfoBase.class));
        return Result.success();
    }

    @ApiOperation(value = "更新实验样本基本表的数据")
    @SystemLog(value = "更新实验样本", logo = "samplesNumber")
    @RequestMapping(value = "/update" , method = RequestMethod.PUT)
    public Result update(@Valid @RequestBody SamInfoBaseParam param){
        log.debug("开始跟新实验样本基本表-->{}",param);
        return Result.success(this.samInfoBaseService.updateById(CommonUtil.copyBean(param,SamInfoBase.class)));
    }

    @ApiOperation(value = "删除实验样本基本表的一条数据")
    @RequestMapping(value = "/delete" , method = RequestMethod.DELETE)
    @ApiImplicitParams(
        @ApiImplicitParam(name= "id" ,value = "数据ID", required = true , example = "-1" )
    )
    public Result delete(@Valid @RequestParam @NotBlank(message = "删除数据ID不能为空") String id){
        log.debug("开始删除实验样本基本表的数据-->{}",id);
        return Result.success(this.samInfoBaseService.removeById(id));
    }

    @ApiOperation(value = "根据条件查询一条实验样本基本表的数据")
    @RequestMapping(value = "/queryForOneRow" , method = RequestMethod.POST)
    public Result<SamInfoBaseVo> queryForOneRow(@Valid @RequestBody SamInfoBaseQueryForOneRowParam param){
        SamInfoBase row = this.samInfoBaseService.getOne(param.convertParamToQueryWapper());
        return Result.success( CommonUtil.copyBean(row, SamInfoBaseVo.class));
    }

    @ApiOperation(value = "根据条件查询实验样本基本表的数据")
    @RequestMapping(value = "/queryList" , method = RequestMethod.POST)
    public Result<Page<SamInfoBaseVo>> queryList(@Valid @RequestBody SamInfoBaseQueryPageParam param){
        QueryWrapper<SamInfoBase> query = SamInfoBaseHelp.createQuery();
        query.eq(SamInfoBaseHelp.groupNumber,param.getGroupNumber());
        return Result.success(this.samInfoBaseService.list(query));
    }

    @ApiOperation(value = "实验样本导出")
    @PostMapping(value = "samInfoExport")
    public Result groupExport(@Valid @RequestBody SamInfoBaseQueryPageParam param
            , HttpServletResponse response) {
        this.samInfoBaseService.samInfoBaseExport(param, response);
        return Result.success();
    }


}
